package com.woniuxy.microsystemserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.anno.SysOperLog;
import com.woniuxy.bean.SysRateBean;
import com.woniuxy.constant.LogConst;
import com.woniuxy.microsystemserver.service.SysRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:  系统费率表现层
 *
 * @Auther: HanLei
 * @Date: 2021/4/7:14:07
 */
@RefreshScope
@Slf4j
@Api(tags = "系统费率制定相关接口")
@RestController
@RequestMapping(value = "system/rate", produces = {"application/json;charset=utf-8"})
public class RateController {

    @Resource
    private SysRateService sysRateServiceImpl;

    /*
     * Description :   获取所有系统费率
     *                 调用SysRateService接口的getAllRate方法调用数据访问层代码获取数据
     * ChangeLog : 1. 创建 (2021/4/7 15:30 );
     * @param null
     * @return String   将查询结果转化为JSON字符串返回
     */
    @ApiOperation("查询所有系统费率")
    @GetMapping("/getRate")
    public ResultBean getRate() {
        List<SysRateBean> allRate = sysRateServiceImpl.getAllRate();
        if (allRate != null) {
            return ResultBean.builder().code(200).msg("查询成功!").data(allRate).build();
        }else {
            return ResultBean.builder().code(500).msg("系统繁忙，请稍后重试!").build();
        }
    }

    /*
     * Description :   设置系统费率
     * ChangeLog : 1. 创建 (2021/4/7 15:32 );
     * @param sysRateBeans  新的费率集合
     * @return String   将查询结果转化为JSON字符串返回
     */
    @SysOperLog(
            action = "更新系统费率",
            operType = LogConst.OperType.UPDATE
    )
    @ApiOperation("更新系统费率")
    @PutMapping("/setRate")
    public ResultBean setRate(@ApiParam("新的系统费率对象集合") @RequestBody List<SysRateBean> sysRateBeans) {
        try{
            int size = sysRateBeans.size();
            if (size > 0) {
                sysRateServiceImpl.updateRate(sysRateBeans);
                return ResultBean.builder().code(200).msg("修改成功,次日00:00:00生效！").build();
            }else {
                return ResultBean.builder().code(500).msg("修改失败，请稍后重试!").build();
            }
        }catch (Exception e) {
            log.error(e.getMessage());
            return ResultBean.builder().code(500).msg("修改失败，请稍后重试!").build();
        }
    }
}
